import { defineStore } from 'pinia'
import { ref } from 'vue'
import router from "@/router/index.js";

export const useUserStore = defineStore(
    'example',
    () => {
        const userInfo = ref()
        const routes = ref()
        const permissions=ref()




        const setUserInfo = ( newUserInfo) => {
                userInfo.value = newUserInfo
        }
        const setRoutes = (newRoutes) => {
            routes.value = newRoutes
        }
        const setPermissions = (newPermissions) => {
            permissions.value = newPermissions
        }



        const removeUserInfo = () => {
            userInfo.value = {}
        }
        const removeRoutes = () => {
            routes.value = []
        }
        const removePermissions = () => {
            permissions.value = []
        }


        // 退出登录时清除用户信息
        const logout = () => {
                removeUserInfo()
                removeRoutes()
                removePermissions()

            router.push('/')
        }

        return {
            userInfo,
            routes,
            permissions,
            setUserInfo,
            setRoutes,
            setPermissions,
            removeUserInfo,
            removeRoutes,
            removePermissions,
            logout,


        }
    },
    {
        persist: true
    }
)